local qinzheng = fk.CreateSkill{
  name = "doublez__qinzheng"
}

Fk:loadTranslationTable{
  ["doublez__qinzheng"] = "勤政",
  [":doublez__qinzheng"] = "你可以将任意张牌当一张【桃】或【洞烛先机】使用，然后若本回合进入弃牌堆的牌每种花色数不相等，此技能本轮失效。",

  ["#doublez__qinzheng"] = "勤政：你可以将任意张牌当一张【桃】或【洞烛先机】使用（目前弃牌堆：%arg）",

  ["$doublez__qinzheng"] = "治疾及其未笃，除患贵其莫深。",
}

local U = require "packages.utility.utility"

qinzheng:addEffect("viewas", {
  pattern = "peach,foresight",
  prompt = function (self, player, selected_cards, selected)
    local cards = table.map(  player:getTableMark("doublez__qinzheng-cards-turn"), Util.Id2CardMapper)
    local suits = {}
    for _, c in ipairs(cards) do
      suits[c.suit] = (suits[c.suit] or 0) + 1
    end
    local p = (suits[Card.Heart] or 0)..Fk:translate(U.ConvertSuit(Card.Heart, "int", "sym"))..","..
    (suits[Card.Spade] or 0)..Fk:translate(U.ConvertSuit(Card.Spade, "int", "sym"))..","..
    (suits[Card.Diamond] or 0)..Fk:translate(U.ConvertSuit(Card.Diamond, "int", "sym"))..","..
    (suits[Card.Club] or 0)..Fk:translate(U.ConvertSuit(Card.Club, "int", "sym"))
    return "#doublez__qinzheng:::"..p
  end,
  card_filter = function (self, player, to_select, selected, selected_targets)
    return true
  end,
  handly_pile = false,
  interaction = function (self, player)
    local all_names = {"peach", "foresight"}
    all_names = player:getViewAsCardNames(qinzheng.name, all_names)
    return UI.CardNameBox{
      choices = all_names
    }
  end,
  view_as = function (self, player, cards)
    if #cards == 0 or not table.contains({"peach", "foresight"}, self.interaction.data) then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(cards)
    card.skillName = qinzheng.name
    return card
  end,
  after_use = function (self, player, use)
    local cards = table.map(player:getTableMark("doublez__qinzheng-cards-turn"), Util.Id2CardMapper)
    local suits = {}
    for _, c in ipairs(cards) do
      suits[c.suit] = (suits[c.suit] or 0) + 1
    end
    if not ( suits[Card.Heart] == suits[Card.Club] and suits[Card.Club] == suits[Card.Spade] and suits[Card.Spade] == suits[Card.Diamond]) then
      player.room:invalidateSkill(player, qinzheng.name, "-round")
    end
  end,
  enabled_at_response = function (self, player, response)
    return not response
  end
})

qinzheng:addEffect(fk.AfterCardsMove, {
  mute = true,
  frequency = Skill.Compulsory,
  can_refresh = function (self, event, target, player, data)
    if (not player:hasSkill(self, true) )or (not player:hasSkill("doublez__qinzheng",true)) then
      return false
    end
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then 
        return true
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          player.room:addTableMarkIfNeed(player, "doublez__qinzheng-cards-turn", info.cardId)
        end
      end
    end
  end
})

qinzheng:addAcquireEffect(function (self, player)
  player.room.logic:getEventsByRule(GameEvent.MoveCards, 1, function (e)
    for _, move in ipairs(e.data) do
      if move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          player.room:addTableMarkIfNeed(player, "doublez__qinzheng-cards-turn",  info.cardId)
        end
      end
    end
  end,Player.HistoryGame)
end)

qinzheng:addLoseEffect(function (self, player)
  player.room:setPlayerMark(player, "doublez__qinzheng-cards-turn", 0)
end)

return qinzheng